﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Yitter.IdGenerator;

namespace SheepCommon.API.SheepCommands
{
    public class SheepArchivesCreateHandler : IRequestHandler<SheepArchivesCreateCommand, Result>
    {
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;
        private readonly IMapper _mapper;

        public SheepArchivesCreateHandler(IBaseRepository<SheepArchives> sheepArchivesRepository, IMapper mapper)
        {
            _sheepArchivesRepository = sheepArchivesRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SheepArchivesCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code=(int)CommonErrorCode.Added};
            try
            {
                SheepArchives sheepArchives = _mapper.Map<SheepArchives>(request);
                sheepArchives.CreatedOwnerId = "张三";//创建人
                sheepArchives.CreatedTime = DateTime.Now;//创建时间
                sheepArchives.IsPregnancyTest = 1;//1代表未妊检,2代表妊检
                sheepArchives.ProductionGrade = 1;//1未定级   生产等级
                sheepArchives.SalesGrade = 1;//未定级         销售等级
                var res = await _sheepArchivesRepository.Add(sheepArchives);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Code = (int)CommonErrorCode.Added;
                    result.Msg = "添加失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
